(function(S) {
	S.declare('Button', {
		focusIndex : 0,
		items : null,
		container : '',
		className : 'simple-btn-container',
		hoverClass : 'simple-btn-hover',
		disableClass : 'simple-btn-disabled',
		focusClass : 'simple-btn-focus',
		templateString : '<a class="simple-btn"><span rel="label"></span></a>',
		init : function() {
			this.buttonEls = [];
			this.container = $(this.container).addClass(this.className);
			this.setButtons(this.items);
		},
		setButtons : function(buttons) {
			if (S.isPlainObject(buttons)) {
				S.each(buttons, function(item, label) {
					var buttonEl = $(this.templateString), self = this;
					buttonEl.find('[rel=label]').text(label);
					buttonEl.bind({
						'click' : function(e) {
							var $this = $(this);
							if (/disable/.test($this.attr('class'))) {
								return false;
							}
							item.call(this, e);
						},
						'mouseover' : function() {
							$(this).addClass(self.hoverClass);
						},
						'mouseout' : function() {
							$(this).removeClass(self.hoverClass);
						}
					});
					this.buttonEls.push(buttonEl);
					this.container.append(buttonEl);
				}, this);
			} else if (S.isArray(buttons)) {
				S.each(buttons, function(item, index) {
					var event = item.event, label = item.label, cls = item.className, icon = item.icon, buttonEl = $(this.templateString);
					buttonEl.find('[rel=label]').text(label);
					buttonEl.addClass(cls);
					if (icon) {
						buttonEl.prepend('<span class="' + icon + '"></span>');
					}
					buttonEl.bind({
						'click' : function(e) {
							var $this = $(this);
							if (/disable/.test($this.attr('class'))) {
								return false;
							}
							event.call(this, e);
						},
						'mouseover' : function() {
							$(this).addClass(self.hoverClass);
						},
						'mouseout' : function() {
							$(this).removeClass(self.hoverClass);
						}
					});
					this.buttonEls.push(buttonEl);
					this.container.append(buttonEl);
				}, this);
			}
		},
		enable : function(index) {
			this.buttonEls[index].removeClass(this.disableClass);
			this.disbaleIndex = null;
		},
		disable : function(index) {
			this.buttonEls[index].addClass(this.disableClass);
			this.disableIndex = index;
		},
		setFocus : function(index) {
			this.buttonEls[index].focus();
			this.buttonEls[index].addClass(this.focusClass);
			this.focusIndex = index;
		},
		getFocus : function() {
			return this.buttonEls[this.focusIndex];
		},
		getDisable : function() {
			if (this.disableIndex) {
				return this.buttonEls[this.disableIndex];
			}
			return null;
		},
		destroy : function() {
			S.each(this.buttonEls, function(el, index) {
				el.unbind();
				el.remove();
			});
			this.buttonEls = null;
			this.inherit(arguments);
		}
	});
})(Simple);
